package com.iad.controller.command
{
  import com.adobe.cairngorm.business.ServiceLocator;
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.adobe.cairngorm.control.CairngormEventDispatcher;
  import com.iad.controller.event.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import mx.rpc.AsyncToken;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;
  import mx.rpc.http.HTTPService;
  
  public class InitializationCommand implements Command
  {
    [Bindable]
    private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
 
    public function execute(event:CairngormEvent):void {
      switch ( event.type ) {
        case InitializationEvent.GET_PROPERTIES:
          getProperties();
        break;
        case SiteEvent.DO_LOGIN:
          doLogin((event as SiteEvent).object);
        break;
     }
    }
    
    
    private function doLogin(data:Object):void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.DO_LOGIN_RESULT, handleLoginResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.doLogin(data.login, data.password);
    }
    
    protected function handleLoginResult(event:ServiceResultEvent):void {
      var user:Site = event.result as Site;
      
      if (user == null) {
        trace('login failed'); 
      }
      else {
        model.user = user;
        trace('login of ' + user.firstName + ' ' + user.lastName + ' [' + user.pathLabel + '] worked'); 
        model.app.adminControls.visible = true;
        model.app.logoutControl.visible = true;
        model.currentSiteLabel = user.pathLabel;
        if (user.id !=  model.ADMIN_ID) {
          CairngormEventDispatcher.getInstance().dispatchEvent(new SiteEvent(SiteEvent.GET_SITE_BY_LABEL, null));
        }
      }
    }
    
     
    private function onError(event:ServiceResultEvent):void {
      trace("error: " + event);
    }
     
     
    private function getProperties():void {
      var service:HTTPService  = ServiceLocator.getInstance().getHTTPService(model.PROPERTIES_SERVICE);
      var token:AsyncToken = service.send();
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getPropertiesOnResult, getPropertiesOnFault);
      token.addResponder(responder);
    }
    
    protected function getPropertiesOnResult(event:ResultEvent):void {
      trace("fetching properties:\n" + event.result);
      model.SERVER = (event.result as XML).server;
      
      model.PORT = (event.result as XML).port;
      if (model.PORT == "80") {
        model.PORT_FORMATTED = "";
      }
      else {
        model.PORT_FORMATTED = ":" + model.PORT;
      }
      
      model.CONTEXT = (event.result as XML).context;
      CairngormEventDispatcher.getInstance().dispatchEvent(new SiteEvent(SiteEvent.GET_SITES, null));
      CairngormEventDispatcher.getInstance().dispatchEvent(new PageEvent(PageEvent.GET_PAGES, 0));
    }
        
        
    protected function getPropertiesOnFault(event:FaultEvent):void {
      trace("ERROR fetching properties:\n" + event.message.toString());
    }
     
  } 
}
    